package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;

public class DVDCtr
{
    private DVDCollection dvdCollection;
    
    public DVDCtr()
    {
      dvdCollection = new DVDCollection();
       
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    
    /**
     * find DVD by its id
     */
    public DVD findDVD(int id)
    {
        return dvdCollection.getDVD(id);
    }
    
    /**
     * Create a new DVD
     */
    public void createDVD(String title, String artist, String releaseDate)
    {       
        int id = 0;
        boolean found = false;
        while (found == false){
            if(findDVD(id) == null || findDVD(id).getId() != id){
                DVD createDVD = new DVD(id, title, artist, releaseDate);
                dvdCollection.addDVD(createDVD);
                found = true;
            }
            else {
                id++;
            }
        }
    }

    /**
     * Delete a DVD by id
     */
    public void deleteDVD(int id)
    {
        if(dvdCollection.getDVD(id) == null){
        }
        else {
        dvdCollection.deleteDVD(id);
        //search for the dvd and delete the object from the container
        }
       
    }
    
    /**
     * Create a copy to a specific DVD
     */
    public void createCopy(int id, String supplyDate, String purchasePrice)
    {
       int serialNumber = 0;
       boolean found = false;
       while (found == false){
                if(findDVD(id) != null) {
                    if(findDVD(id).getCopy(serialNumber) == null || findDVD(id).getCopy(serialNumber).getSerialNumber() != serialNumber) {
                        Copy copy = new Copy(serialNumber, supplyDate, purchasePrice);
                        findDVD(id).addCopies(copy);
                        found = true;
                    }
                    else {
                        serialNumber++;
                        
                    }
            }
            else {
                found = true;
            } 
       }
    }
    
    public Copy getCopy(int serialNumber, int id)
    {
        return dvdCollection.getCopy(serialNumber, id);
    }
    /**
     * List all dvds
     */
    public ArrayList listAllDvds()
    {
        return dvdCollection.listAllDvds();
    }
    
    public ArrayList listAllCopies(int id)
    {
        return dvdCollection.listAllCopies(id);
    }
}
